﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebHoiChoMuaBan.Controllers.Helpers;
using WebHoiChoMuaBan.Controllers.Private;
using WebHoiChoMuaBan.Models;

namespace WebHoiChoMuaBan.Controllers.Admin
{
    public class AdminController : Controller
    {
        private MenuLeft menuLeft;
        private int MemId;
        public AdminController()
        {
            LoadMainMenu();
            //InitMenuLeft(int.Parse(Session["MemId"]+""));
            // InitMenuLeft(int.Parse(Session["MemId"]+""));
        }

        public bool IsLogin
        {
            get
            {
                if (Session["IsLogin"] == null)
                    return false;
                else return (bool)Session["IsLogin"];
            }
        }

        protected override void Initialize(System.Web.Routing.RequestContext requestContext)
        {
            base.Initialize((System.Web.Routing.RequestContext)requestContext);
            object tmp = requestContext.HttpContext.Session["MemId"];
            if (!IsLogin)
                HomeAdminPage();
            else
            {
                this.MemId = (int)tmp;
                this.LoadMainMenu();
                this.InitMenuLeft(this.MemId);
            }
        }

        protected bool checkRight()
        {
            //Ham nay se hoan thien sau khi hoan tat phan quyen
            if (Session["MemId"] != null)
            {
                ViewData["IsOwner"] = true;
                return true;
            }
            else
            {
                ViewData["IsOwner"] = false;
                return false;
            }
        }

        protected ActionResult HomeAdminPage()
        {
            return RedirectToRoute("Default", new { controller = "Home", action = "Index" });
        }

        protected ActionResult HomePage()
        {
            return View("~/Index.aspx");
        }

        protected void LoadMainMenu()
        {
            PageTypeHelper hPage = new PageTypeHelper();
            ViewData["MainMenu"] = hPage.getAllPageTypes();
        }

        protected ActionResult LoadMember(int id)
        {
            //Load Member có uid này
            MemberHelper hMem = new MemberHelper(id);
            if (hMem.Member == null)
                return HomeAdminPage();

            if (Session["MemId"] != null && hMem.Member.MemId == (int)Session["MemId"])
                ViewData["IsOwner"] = true;
            else
                ViewData["IsOwner"] = false;
            ViewData["hMem"] = hMem;

            return null;
        }

        public void LoadNewsType()
        {
            NewsTypeHelper newsTypeHelper = new NewsTypeHelper();
            ViewData["ListNewsType"] = newsTypeHelper.GetAllNewsType();
        }

        public void LoadCateLevel_1()
        {
            CategoryHelper cateHelper = new CategoryHelper();
            ViewData["CateLevel_1"] = cateHelper.GetListParentCategory();
        }

        public void InitMenuLeft(int memid)
        {
            StringMenu.LINK[] quanLyThanhVien;
            int id;
            StringMenu.LINK[] quanlysanpham;
            CategoryHelper cateHelper;
            List<Category> categories;
            NodeMenu nodeOfQLSP;
            List<Category> lstCateLevel_2;
            int jID;
            string link;
            CategoryHelper newCatHelper;
            List<Category> parrNewsCat;
            this.menuLeft = new MenuLeft();
            Member member = new MemberHelper().getMember(memid);
            Group group = new AdminHelper().getGroupByMemberId(member.MemId);
            List<Group_Right> groupRight = new AdminHelper().getAllGroupRightByGroupId(group.GroupID);
            List<Right> Right = new AdminHelper().getAllRightByGroupId(group.GroupID);
            List<Template> Template = new AdminHelper().getAllTemplatebyGroupAndRight(group.GroupID, Right);
            List<int> RightId = new List<int>();
            foreach (Right r in Right)
            {
                RightId.Add(int.Parse(r.RightName));
            }
            string[] strLevel1 = new string[] { "Quản lý thành viên", "Quản lý sản phẩm", "Quản lý tin tức", "Quản lý quảng cáo", "Tạo danh mục", "Add temp", "Quản lý riêng", "Quản lý admin" };
            if ((RightId.Count == 1) && (RightId[0] == 1))
            {
                int idLevel1 = 0;
                foreach (string node in strLevel1)
                {
                    if (node == "Quản lý admin")
                    {
                        this.menuLeft.AddNode(new NodeMenu(idLevel1.ToString(), node, "/AdminManagement/Index"));
                    }
                    else if (node == "Quản lý riêng")
                    {
                        this.menuLeft.AddNode(new NodeMenu(idLevel1.ToString(), node, "/PrivateManager/Index"));
                    }
                    else if (node == "Tạo danh mục")
                    {
                        this.menuLeft.AddNode(new NodeMenu(idLevel1.ToString(), node, "/CreateCategory/Index"));
                    }
                    else
                    {
                        this.menuLeft.AddNode(new NodeMenu(idLevel1.ToString(), node));
                    }
                    idLevel1++;
                }
                quanLyThanhVien = new StringMenu.LINK[] { StringMenu.QUAN_LY_THANH_VIEN.THANH_VIEN_MOI, StringMenu.QUAN_LY_THANH_VIEN.THANH_VIEN_VANG, StringMenu.QUAN_LY_THANH_VIEN.THANH_VIEN_BI_KHOA, StringMenu.QUAN_LY_THANH_VIEN.MAIL };
                id = 0;
                foreach (StringMenu.LINK node in quanLyThanhVien)
                {
                    this.menuLeft.Nodes("Quản lý thành viên").ListNodeChild.Add(new NodeMenu("0" + id.ToString(), node.Title, node.Link));
                    id++;
                }
                quanlysanpham = new StringMenu.LINK[] { StringMenu.QUAN_LY_SAN_PHAM.SAN_PHAM_NOI_BAT };
                id = 0;
                foreach (StringMenu.LINK node in quanlysanpham)
                {
                    this.menuLeft.Nodes("Quản lý sản phẩm").ListNodeChild.Add(new NodeMenu("0" + id.ToString(), node.Title, node.Link));
                    id++;
                }
                cateHelper = new CategoryHelper();
                categories = cateHelper.getAllParrentProductCategory();
                if (categories != null)
                {
                    foreach (Category category in categories)
                    {
                        nodeOfQLSP = new NodeMenu("1" + id.ToString(), category.CateName, "javascript:void(0)");
                        lstCateLevel_2 = cateHelper.GetChildCategoryById(category.CateId);
                        jID = 0;
                        foreach (Category subCate in lstCateLevel_2)
                        {
                            link = "/AdminProduct/ListProduct?cateId=" + subCate.CateId;
                            nodeOfQLSP.ListNodeChild.Add(new NodeMenu("1" + id.ToString() + jID.ToString(), subCate.CateName, link));
                            jID++;
                        }
                        this.menuLeft.Nodes("Quản lý sản phẩm").ListNodeChild.Add(nodeOfQLSP);
                        id++;
                    }
                }
                List<NewsType> listNewsType = new NewsTypeHelper().GetAllNewsType();
                newCatHelper = new CategoryHelper();
                parrNewsCat = newCatHelper.getAllParrentNewsCategory();
                if (parrNewsCat != null)
                {
                    id = 0;
                    foreach (Category category in parrNewsCat)
                    {
                        nodeOfQLSP = new NodeMenu("2" + id.ToString(), category.CateName, "javascript:void(0)");
                        lstCateLevel_2 = cateHelper.GetChildCategoryById(category.CateId);
                        jID = 0;
                        foreach (Category subCate in lstCateLevel_2)
                        {
                            link = "/AdminNews/ListNew?cateId=" + subCate.CateId;
                            nodeOfQLSP.ListNodeChild.Add(new NodeMenu("2" + id.ToString() + jID.ToString(), subCate.CateName, link));
                            jID++;
                        }
                        this.menuLeft.Nodes("Quản lý tin tức").ListNodeChild.Add(nodeOfQLSP);
                        id++;
                    }
                }
                if (listNewsType != null)
                {
                    id = 0;
                    foreach (NewsType nodeType in listNewsType)
                    {
                        link = "/AdminNews?newsTypeId=" + nodeType.NewsTypeId;
                        this.menuLeft.Nodes("Quản lý tin tức").ListNodeChild.Add(new NodeMenu("3" + id.ToString(), nodeType.NewsTypeName, link));
                        id++;
                    }
                }
                StringMenu.LINK[] quanlyQuangCao1 = new StringMenu.LINK[] { StringMenu.QUAN_LY_QUANGCAO.THEM_MOI_KHACH_HANG, StringMenu.QUAN_LY_QUANGCAO.DANH_SACH_KHACH_HANG, StringMenu.QUAN_LY_QUANGCAO.TAO_QUANG_CAO, StringMenu.QUAN_LY_QUANGCAO.DANH_SACH_BANNER };
                int sid = 0;
                foreach (StringMenu.LINK node in quanlyQuangCao1)
                {
                    this.menuLeft.Nodes("Quản lý quảng cáo").ListNodeChild.Add(new NodeMenu("4" + sid.ToString(), node.Title, node.Link));
                    sid++;
                }
            }
            else
            {
                List<int> iTmpStore = new List<int>();
                if (!RightId.Contains(StringMenu.QUAN_LY_THANH_VIEN.iRight))
                {
                    iTmpStore.Add(0);
                }
                else
                {
                    quanLyThanhVien = new StringMenu.LINK[] { StringMenu.QUAN_LY_THANH_VIEN.THANH_VIEN_MOI, StringMenu.QUAN_LY_THANH_VIEN.THANH_VIEN_VANG, StringMenu.QUAN_LY_THANH_VIEN.THANH_VIEN_BI_KHOA, StringMenu.QUAN_LY_THANH_VIEN.MAIL };
                    id = 0;
                    foreach (StringMenu.LINK node in quanLyThanhVien)
                    {
                        this.menuLeft.Nodes("Quản lý thành viên").ListNodeChild.Add(new NodeMenu("0" + id.ToString(), node.Title, node.Link));
                        id++;
                    }
                }
                if (!RightId.Contains(StringMenu.QUAN_LY_SAN_PHAM.iRight))
                {
                    iTmpStore.Add(1);
                }
                else
                {
                    this.menuLeft.AddNode(new NodeMenu("0", "Quản lý sản phẩm", "/AdminManagement/Index"));
                    quanlysanpham = new StringMenu.LINK[] { StringMenu.QUAN_LY_SAN_PHAM.SAN_PHAM_NOI_BAT };
                    id = 0;
                    foreach (StringMenu.LINK node in quanlysanpham)
                    {
                        this.menuLeft.Nodes("Quản lý sản phẩm").ListNodeChild.Add(new NodeMenu("0" + id.ToString(), node.Title, node.Link));
                        id++;
                    }
                    cateHelper = new CategoryHelper();
                    categories = cateHelper.getAllParrentProductCategoryByTemplate(Template);
                    if (categories != null)
                    {
                        foreach (Category category in categories)
                        {
                            nodeOfQLSP = new NodeMenu("1" + id.ToString(), category.CateName, "javascript:void(0)");
                            lstCateLevel_2 = cateHelper.GetChildCategoryById(category.CateId);
                            jID = 0;
                            foreach (Category subCate in lstCateLevel_2)
                            {
                                link = "/AdminProduct/ListProduct?cateId=" + subCate.CateId;
                                nodeOfQLSP.ListNodeChild.Add(new NodeMenu("1" + id.ToString() + jID.ToString(), subCate.CateName, link));
                                jID++;
                            }
                            this.menuLeft.Nodes("Quản lý sản phẩm").ListNodeChild.Add(nodeOfQLSP);
                            id++;
                        }
                    }
                }
                if (!RightId.Contains(StringMenu.QUAN_LY_TIN_TUC.iRight))
                {
                    iTmpStore.Add(2);
                }
                else
                {
                    this.menuLeft.AddNode(new NodeMenu("0", "Quản lý tin tức", "/AdminManagement/Index"));
                    newCatHelper = new CategoryHelper();
                    parrNewsCat = newCatHelper.getAllParrentNewsCategory();
                    if (parrNewsCat != null)
                    {
                        id = 0;
                        foreach (Category category in parrNewsCat)
                        {
                            nodeOfQLSP = new NodeMenu("2" + id.ToString(), category.CateName, "javascript:void(0)");
                            lstCateLevel_2 = newCatHelper.GetChildCategoryById(category.CateId);
                            foreach (Category subCate in lstCateLevel_2)
                            {
                                link = "/AdminNews/ListNew?cateId=" + subCate.CateId;
                                nodeOfQLSP.ListNodeChild.Add(new NodeMenu("2" + id.ToString() + id.ToString(), subCate.CateName, link));
                                id++;
                            }
                            this.menuLeft.Nodes("Quản lý tin tức").ListNodeChild.Add(nodeOfQLSP);
                            id++;
                        }
                    }
                }
                if (!RightId.Contains(StringMenu.QUAN_LY_QUANGCAO.iRight))
                {
                    iTmpStore.Add(3);
                }
                else
                {
                    this.menuLeft.AddNode(new NodeMenu("0", "Quản lý quảng cáo", "/AdminManagement/Index"));
                    StringMenu.LINK[] quanlyQuangCao = new StringMenu.LINK[] { StringMenu.QUAN_LY_QUANGCAO.THEM_MOI_KHACH_HANG, StringMenu.QUAN_LY_QUANGCAO.DANH_SACH_KHACH_HANG, StringMenu.QUAN_LY_QUANGCAO.TAO_QUANG_CAO, StringMenu.QUAN_LY_QUANGCAO.DANH_SACH_BANNER };
                    int jid = 0;
                    foreach (StringMenu.LINK node in quanlyQuangCao)
                    {
                        this.menuLeft.Nodes("Quản lý quảng cáo").ListNodeChild.Add(new NodeMenu("1" + jid.ToString(), node.Title, node.Link));
                        jid++;
                    }
                }
                if (!RightId.Contains(StringMenu.QUAN_LY_TAO_DANH_MUC.iRight))
                {
                    iTmpStore.Add(4);
                }
                else
                {
                    this.menuLeft.AddNode(new NodeMenu("0", "Tạo danh mục", "/CreateCategory/Index"));
                }
                if (!RightId.Contains(StringMenu.QUAN_LY_ADD_TEMP.iRight))
                {
                    iTmpStore.Add(5);
                }
                if (!RightId.Contains(StringMenu.QUAN_LY_RIENG.iRight))
                {
                    iTmpStore.Add(6);
                }
                else
                {
                    this.menuLeft.AddNode(new NodeMenu("0", "Quản lý riêng", "/PrivateManager/Index"));
                }
                if (!RightId.Contains(StringMenu.QUAN_LY_ADMIN.iRight))
                {
                    iTmpStore.Add(7);
                }
                else
                {
                    this.menuLeft.AddNode(new NodeMenu("0", "Quản lý admin", "/AdminManagement/Index"));
                }
                for (int i = 0; i < iTmpStore.Count; i++)
                {
                    strLevel1[iTmpStore[i]] = "null";
                }
                strLevel1 = (from w in strLevel1
                             where w != "null"
                             select w).ToArray<string>();
            }
            string htmlStringMenu = string.Empty;
            if (this.menuLeft.Count != 0)
            {
                htmlStringMenu = "<ul>\n";
                htmlStringMenu = htmlStringMenu + this.GenerateHtml(this.menuLeft.GetNodes()) + "</ul>\n";
            }
            base.ViewData["HtmlStringMenu"] = htmlStringMenu;
            base.ViewData["MenuLeft"] = this.menuLeft;
        }

        private String GenerateHtml(List<NodeMenu> listNode)
        {
            String temp = String.Empty;

            foreach (NodeMenu node in listNode)
            {
                if (node.ListNodeChild.Count == 0)
                {
                    temp += "<li id='li" + node.NodeID + "'><a href=\"" + node.NodeLink
                        + "\" onclick=\"SendEventOnClick('" + node.NodeID + "')\">" + node.NodeName + "</a></li>\n";
                }
                else
                {
                    temp += "<li id='li" + node.NodeID
                        + "'><a href='javascript:void(0)' onclick=\"Expand_Collapse('"
                        + node.NodeID + "')\">" + node.NodeName + "</a>\n"
                        + "<ul id='ul" + node.NodeID + "' style='display:none;'>\n";
                    temp += GenerateHtml(node.ListNodeChild);
                    temp += "</ul>\n</li>\n";
                }
            }
            return temp;
        }
    }
}
